package com.csair.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.csair.common.AppUtilsConfig;

import java.util.Map;

public final class SharedPreferencesUtil {
    /***
     * Key串
     */
    public static final String HASUPDATE = "HASUPDATE";
    public static final String ISSHOWDOWNLOADDIALOG = "ISSHOWDOWNLOADDIALOG";
    public static final String LOCATION_BAIDU = "LOCATION_BAIDU";
    public static final String COUPON_SALES = "COUPON_SALES";//消息中心--优惠促销
    public static final String SERVICE_NOTICE = "SERVICE_NOTICE";//消息中心--服务公告
    public static final String TAGJPDD = "TAGJPDD";//消息中心--客票订单
    public static final String IS_LOAD_SETTING = "IS_LOAD_SETTING";//消息中心--是否加载设置状态的开关标记


    private static SharedPreferencesUtil instance;
    private static SharedPreferences mSharedPreferences;
    private static Context mContext;
//    private static ISharepreferenceSec mISharepreferenceSec;

    /**
     * @param context 在Application中初始化SharedPreferencesUtil
     */
    public static void initSharedPreferencesUtil(Context context){
        mContext = context;
//        mISharepreferenceSec = SecurityFactory.getStorageSec(context,SecurityFactory.SECURITY_ALI);
        if (instance == null) {
            synchronized (SharedPreferencesUtil.class) {
                if (instance == null) {
                    instance = new SharedPreferencesUtil();
                }
            }
        }
    }

    /***
     * @return 获取SharedPreferencesUtil的单例实例
     */
    public synchronized static SharedPreferencesUtil getInstance() {
        mSharedPreferences = getSharedPreferencesObj();
        return instance;
    }
    public synchronized static SharedPreferencesUtil getInstance(String preferenceFileName) {
        mSharedPreferences = getSharedPreferencesObj(preferenceFileName);
        return instance;
    }
    public synchronized static SharedPreferences getSharePreferencesSingleton(){
        return mSharedPreferences;
    }
    /*************************************/

    /***
     * @param preferenceFileName sp文件名
     * @return  根据文件名返回对应sp实例
     */
    private static SharedPreferences getSharedPreferencesObj(String preferenceFileName) {
        if(!TextUtils.isEmpty(preferenceFileName)){
            mSharedPreferences = mContext.getApplicationContext().getSharedPreferences(preferenceFileName, Context.MODE_PRIVATE);
        } else{
            mSharedPreferences = mContext.getApplicationContext().getSharedPreferences(AppUtilsConfig.PREFERENCE_DEFAULT_FILENAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences;
    }
    private static SharedPreferences getSharedPreferencesObj() {
        mSharedPreferences = mContext.getApplicationContext().getSharedPreferences(AppUtilsConfig.PREFERENCE_DEFAULT_FILENAME, Context.MODE_PRIVATE);
        return mSharedPreferences;
    }
    /*************************************/

    /**
     * @return 以Map格式返回指定的整份SharedPreferences
     */
    public Map<String, ?> getMapFromSp(){
        return mSharedPreferences.getAll();
    }
//    /**
//     * 保存加密信息
//     */
//    public void saveSecData(String key, String val){
//        mISharepreferenceSec.putKeyVal(key,val);
//    }
//    /**
//     * 获取加密信息
//     */
//    public String getSecData(String key){
//        return mISharepreferenceSec.getKeyVal(key);
//    }
//    /**
//     * 清除加密信息
//     */
//    public void removeSecData(String key){
//        mISharepreferenceSec.removeKeyVal(key);
//    }



    /**
     * 保存字符串
     */
    public void saveString(String key, String value) {
        mSharedPreferences.edit().putString(key, value).apply();
    }

    /**
     * 获取字符串
     */
    public String getString(String key, String... defValue) {
        if (defValue.length > 0) {
            return mSharedPreferences.getString(key, defValue[0]);
        } else {
            return mSharedPreferences.getString(key, "");//显示设置会员服务开关
        }
    }

    /**
     * 获取布尔值
     */
    public Boolean getBoolean(String key, Boolean... defValue) {
        if (defValue.length > 0) {
            return mSharedPreferences.getBoolean(key, defValue[0]);
        } else {
            return mSharedPreferences.getBoolean(key, false);
        }
    }

    /**
     * 保存布尔值
     */
    public void saveBoolean(String key, Boolean defValue) {
        mSharedPreferences.edit().putBoolean(key, defValue).apply();
    }

    /**
     * 保存 Integer
     */
    public void saveInteger(String key, Integer defValue) {
        mSharedPreferences.edit().putInt(key, defValue).apply();
    }

    /**
     * 取 Integer 值
     */
    public Integer getInteger(String key, Integer... defValue) {
        if (defValue.length > 0) {
            return mSharedPreferences.getInt(key, defValue[0]);
        }
        return mSharedPreferences.getInt(key, 0);
    }

    /**
     * 保存 Long
     */
    public void saveLong(String key, Long defValue) {
        mSharedPreferences.edit().putLong(key, defValue).apply();
    }


    /**
     * 取 Long 值
     */
    public Long getLong(String key, Long... defValue) {
        if (defValue.length > 0) {
            return mSharedPreferences.getLong(key, defValue[0]);
        }
        return mSharedPreferences.getLong(key, 0);
    }

    /**
     * 保存一个object
     */
    public <T> void saveObject(String key, T obj) {
        if (obj != null) {
            mSharedPreferences.edit().putString(key, JsonUtil.beanToString(obj)).apply();
        } else {
            mSharedPreferences.edit().putString(key, "").apply();
        }
    }

    /**
     * 返回一个object
     */
    public <T> T getObject(String key, Class<T> cls) {
        String jsonStr = mSharedPreferences.getString(key, "");
        if (TextUtils.isEmpty(jsonStr)) return null;
        return JsonUtil.stringToBean(jsonStr,cls);
    }

    /**
     * 保存一个map
     */
    public void saveMap(String key, Map obj) {
        if (obj != null) {
            String jsonStr = JsonUtil.beanToString(obj);
            mSharedPreferences.edit().putString(key, jsonStr).apply();
        } else {
            mSharedPreferences.edit().putString(key, "").apply();
        }
    }

    /**
     * 返回一个map
     */
    public Map getMap(String key) {
        String jsonStr = mSharedPreferences.getString(key, "");
        if (TextUtils.isEmpty(jsonStr)) return null;
        return JsonUtil.stringToBean(jsonStr, Map.class);
    }
}
